<?php
namespace Tbf\Bundle\PackageBundle;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Tbf\Bundle\TbfBundle\Controller\TbfController;

/**
 * 这个controller作为一个service,自己对自己进行配置
 */
abstract class AbstractController extends TbfController implements ControllerConfigInterface
{
    protected $service_id;
    /**
     * 路由配置
     * 如:
     *          return array(
     * 'list'=>'@listAction',
     * 'create'=>'@createAction',
     * 'update'=>'@updateAction',
     * 'delete'=>'@deleteAction',
     * );
     * @return map[string]string
     */
    abstract protected function getRouteConfig();

    /**
     * controller的serviceId
     * @return string
     */
    public function getControllerServiceId()
    {
        return $this->service_id;
    }
    public function setControllerServiceId($service_id)
    {
        $this->service_id = $service_id;
    }
    /**
     * 路由前缀
     * @return string
     */
    protected function getRoutePrefix()
    {
        return '/';
    }

    /**
     * 路由名称的前缀,路由名称可能没有什么用,但是要唯一
     * @return string
     */
    protected function getRouteNamePrefix()
    {
        return $this->getControllerServiceId().'_';
    }
    /**
     * @return \Symfony\Component\Routing\RouteCollection
     */
    public function getRouteCollection()
    {
        $collection = new RouteCollection();
        foreach ($this->getRouteConfig() as $name=>$config_method) {
            if (strpos($config_method,'@')===0) {
                $method = $this->getControllerServiceId().':'.substr($config_method,1);
            } else {
                $method = $config_method;
            }
            $collection->add($this->getRouteNamePrefix().$name, new Route('/'.$name, array(
                '_controller' => $method,
            )));
        }
        $collection->addPrefix($this->getRoutePrefix());

        return $collection;
    }

}
